﻿module Common

open System.IO
open System

///地址是不是目录
let isDir (url:string) =
    //拖拽进来的肯定是存在的，如果不存在就说明不是目录
    Directory.Exists(url)

///可以递归生成不存在的目录,目录已存在（或已有目录且有文件）就不会创建
let makeDir (dir:string)=
    Directory.CreateDirectory(dir)  

let getDirFiles( dir:string) =
    Directory.GetFiles dir
let getDirFilesExt( dir:string,searchPtn:string,option:SearchOption) =
    Directory.GetFiles( dir,searchPtn,option)
///获取全路劲的目录的目录名
let getDirByDirPath (dirPath:string) =
    let i = dirPath.LastIndexOf('\\')
    dirPath.Substring(i+1)
///获取文件的目录
let getDir (filePath:string) =
    if filePath.Contains(".") then
        let (lst,i) =filePath |> Seq.fold (fun (indexs,i) c -> if c = '\\' then (i::indexs,i+1) else (indexs,i+1) ) ([],0)
        filePath.Substring(0,lst.Head)
    else filePath
let getFileName (fileFullPath:string) =
        let (lst,i) =fileFullPath |> Seq.fold (fun (indexs,i) c -> if c = '\\' then (i::indexs,i+1) else (indexs,i+1) ) ([],0)
        fileFullPath.Substring(lst.Head+1)
let getFileExtension (filePath:string) =
    let i = filePath.LastIndexOf('.')
    filePath.Substring(i)
let (|BothHave|_|) (a:string,b:string) = 
    if a.[a.Length-1] = '\\' && b.[0]='\\' then
        Some(a+b.Substring(1))
    else None
let (|BothNone|_|) (a:string,b:string) = 
    if a.[a.Length-1] <> '\\' && b.[0]<>'\\' then
        Some(a+"\\"+b)
    else None
let conbindPath path1 path2 =
    match path1,path2 with
    | BothHave str ->str
    | BothNone str ->str
    | _ -> path1+path2
///FTP服务端根据工作项的日期构建目录（只是部分），前需加客户名，后需加模板名
let toDirString( d:DateTime) =
    d.Year.ToString()+"-"+d.Month.ToString()+"-"+d.Day.ToString()+"\\"+d.Hour.ToString()+"-"+d.Minute.ToString()+"-"+d.Second.ToString()

let changeFileNameInplace (fullpath:string,map:Func<string,string>) =
     match ( getFileName fullpath).Split([|'.'|]) with
     |  [|name;ext|] ->
        fullpath.Replace(name,map.Invoke(name))
     | _ -> ""

